'''
Created on Feb 16, 2011

@author: oabalbin
'''
from exome.vcftools.vcf import *
def write_vcf_line(snp, infotags, sampletags):
    '''
    '''
    site=snp['COORD'].split('@')
    CHROM, POS=str(site[0]),str(site[1])
    
    qinfo,asinfo=deque(),deque()
    for tag in infotags:
        if tag not in snp.keys():
            qinfo.append(tag+'=$$')
            continue
        if tag=='CI95' or tag=='SF':
            b=tag+'='
            for j,a in enumerate(snp[tag]):
                if j==0:
                    b+=str(a)
                else:
                    b+='&'+str(a)
            qinfo.append(b)
        else:
            qinfo.append(tag+'='+str(snp[tag]))
            
    '''
    if extra_info is not None:
        for etag,value in extra_info.iteritems():
            qinfo.append(etag+'='+str(value))
    '''
    
    qinfo = [",".join(qinfo).replace(',',';')]
    # snps samples
    samples = snp['_SAMPLES']
    gt={'ref':'0','alt':'1'}
    #print samples
    for sp in samples:
        sinfo=deque()
        #print sp
        for tag in sampletags:
            if tag=="GT":
                gtref=gt[sp['GT1']]
                gtalt=gt[sp['GT2']]
                sinfo.append(gtref+'/'+gtalt)
                continue
            elif tag=="AD":
                ADref=str(sp['ADref'])
                ADalt=str(sp['ADalt'])
                sinfo.append(ADref+'&'+ADalt)
                continue
            
            elif tag=="GL":
                GLrr = str(sp['GLrr'])
                GLra = str(sp['GLra'])
                GLaa = str(sp['GLaa'])
                sinfo.append(GLrr+'&'+GLra+'&'+GLaa)
                continue
            
            sinfo.append(str(sp[tag]))
        asinfo.append((",".join(sinfo).replace(',',':')))
    
    sampletags = [",".join(sampletags).replace(',',':')]
                    
    gralinfo = [CHROM, 
            POS,
            snp['ID'], 
            snp['REF'], 
            snp['ALT'], 
            str(snp['QUAL']), 
            snp['FILTER']]
    
    line = gralinfo + qinfo + sampletags + list(asinfo)
    return ",".join(line).replace(',','\t').replace('&',',')

